/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.com.jlib.ev.persistence.dialect;

import java.sql.Connection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

/**
 *
 * @author afonso.rodrigues
 */
public class BaseDaoImpl implements BaseDao {

    protected EntityManager entityManager;
    private EntityTransaction transaction;

    @Override
    public void beginTransaction() throws Exception {
        transaction = entityManager.getTransaction();
        if (!transaction.isActive()) {
            transaction.begin();
        }
    }

    @Override
    public void commit() throws Exception {
        if (transaction.isActive()) {
            transaction.commit();
        }
    }

    @Override
    public void rollback() throws Exception {
        if (transaction.isActive()) {
            transaction.rollback();
        }
    }

    @Override
    public void close() throws Exception {
        if (transaction != null) {
            if (transaction.isActive()) {
                transaction.commit();
            }
        }
        entityManager.close();
    }

    @Override
    public void flush() throws Exception {
        entityManager.flush();
    }

    @Override
    public void save(Object obj) throws Exception {
        entityManager.persist(obj);
    }

    @Override
    public void update(Object obj) throws Exception {
        entityManager.merge(obj);
    }

    @Override
    public List list(Object obj) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(Object obj) throws Exception {
        entityManager.remove(obj);
    }

    @Override
    public List list(Class<?> c) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void saveOrUpdate(Object obj) throws Exception {
         entityManager.persist(obj);
    }

    @Override
    public Object findObject(Class<?> o, Long id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Connection getConexao() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Query criaQuery(String hql) {
        Query query = entityManager.createQuery(hql);
        return query;
    }

    @Override
    public Query criaQueryNativa(String sql) {
        Query query = entityManager.createNativeQuery(sql);
        return query;
    }

    @Override
    public List<?> executaQuery(Query query) {
             return query.getResultList();
    }

    @Override
    public List<?> find(String hql) throws Exception {
        Query query = entityManager.createQuery(hql);
        return query.getResultList();
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}
